/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unidad.informe;

import DBMS.DataBaseManager;

import clases.Informe;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author carlo
 */
public class ConsultarInforme extends DispatchAction {


    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private static final String VERTODOS = "vertodos";

    public ActionForward one(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
/*
        System.out.println("Descargando el informe");
        //DataBaseManager dbms = DataBaseManager.getInstance();
        Informe informe = (Informe) form;
        DataBaseManager dbm = DataBaseManager.getInstance();
        String ruta = dbm.consultarRutaInforme(informe);

        if (ruta==null) {
            informe.setMensaje("No se ha podido realizar la descarga.");
            request.setAttribute("informe", informe);
            return mapping.findForward(FAILURE);
        }

        //****************
try {
   String filename = "Informe.pdf";

   // set the http content type to "APPLICATION/OCTET-STREAM
   response.setContentType("APPLICATION/OCTET-STREAM");

   // initialize the http content-disposition header to
   // indicate a file attachment with the default filename
   // "myFile.txt"
   String disHeader = "Attachment;\nFilename=\"informe.pdf\"";
   response.setHeader("Content-Disposition", disHeader);

   // transfer the file byte-by-byte to the response object
   File fileToDownload = new File(filename);
   FileInputStream fileInputStream = new FileInputStream(fileToDownload);
   int i;
   while ((i=fileInputStream.read())!=-1)
   {
      out.write(i);
   }
   fileInputStream.close();
   out.close();
   }catch(Exception e) // file IO errors
   {
   e.printStackTrace();
}        
        
        
 //*********************       
        informe.setMensajeOK("Descarga realizada con exito.");
        request.setAttribute("informe", informe);*/
        return mapping.findForward(SUCCESS);
    }

    public ActionForward all(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        //DataBaseManager dbms = DataBaseManager.getInstance();
        //Informe informe = (Informe) form;
        Informe informe = new Informe();
        DataBaseManager dbm = DataBaseManager.getInstance();

        ArrayList<Informe> resultadoConsulta = dbm.consultarInformesPublicos(informe);

        if (resultadoConsulta.isEmpty()) {
            informe.setMensaje("No se ha podido realizar la consulta.");
            request.setAttribute("informe", informe);
            return mapping.findForward(FAILURE);
        }

        informe.setMensajeOK("Consulta realizada con exito.");
        request.setAttribute("informes", resultadoConsulta);
        return mapping.findForward(SUCCESS);
    }
}
